﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NP.BL;

namespace ConsoleApplication1
{
    public class MenuItem
    {
        public int Order { get; set; }
        public string DisplayName { get; set; }
        public Action Action { get; set; }
        public Action Back { get; set; }
    }

    public class MenuManager
    {
        public MenuItem[] menu;

        public MenuManager(MenuItem[] menu)
        {
            this.menu = menu;
        }

        public void DisplayMenu()
        {
            Console.WriteLine();
            Console.WriteLine("BAZA NORTHWIND");
            Console.WriteLine();
            Console.WriteLine("MENU");
            Console.WriteLine("".PadLeft(30, '-'));
            foreach (var item in menu.OrderBy(n => n.Order))
            {
                Console.WriteLine("{0} - {1}", item.Order, item.DisplayName);
            }
        }

        public bool Parse()
        {
            bool res = true;

            Console.WriteLine();
            Action text = () => Console.WriteLine("Podales zla wartosc. Dobre wartosci to: {0}",
                        menu.Aggregate("", (n, m) => n + m.Order + ", "));

            var key = Console.ReadLine();
            int keyInt;
            if (int.TryParse(key, out keyInt))
            {
                var item = menu.SingleOrDefault(n => n.Order == keyInt);

                if (item == null)
                {
                    text();
                }
                else
                {
                    item.Action();
                    res = false;
                }
            }
            else
            {
                text();
            }

            return res;
            
        }

        public void Run()
        {
            bool res;
            do
            {
                DisplayMenu();
                Console.WriteLine();
                Console.WriteLine("Wprowadz wartosc");
                res = Parse();
                
            }
            while (res);
        }
    }
    
    class Program
    {
        static MenuManager menu;

        static void Main(string[] args)
        {
            // zrobic CRUD dla Customers

           

            EmployeesSummary es = new EmployeesSummary();
            IEnumerable<EmployeeSummaryViewModel> esvm = es.GetEmployee();
            //Methods.DisplayEmployeesSummary(esvm);

            ProductsTopDiscounts ptd = new ProductsTopDiscounts();
            ProductTopDiscountViewModel[] ptdvm = ptd.GetTopDiscouts();
            //Methods.DisplayProductsTopDiscounts(ptdvm);

            TotalProductsSold tps = new TotalProductsSold();
            TotalProductSoldViewModel[] tpsvm = tps.TotalProducts();
            //Methods.DisplayTotalProductsSold(tpsvm);

            UnitsInStock uis = new UnitsInStock();
            UnitsInStockViewModel[] uisvm = uis.GetStock();
            //Methods.DisplayUnitsInStock(uisvm);

            CustomersSummary cs = new CustomersSummary();
            CustumerSummaryViewModel[] csvm = cs.GetCustomers();
            //Methods.DisplayCustomersSummary(csvm);

            CRUD crud = new CRUD();
            //crud.ReadCustomers();

            //foreach (var i in ptdvm)
            //{
            //    Console.WriteLine(i.TotalDiscout);
            //}

            //foreach (var i in esvm)
            //{
            //    Console.WriteLine(i.TotalSold);
            //}



           menu = new MenuManager(new MenuItem[]
           
            {


                new MenuItem
                {
                    Order = 1,
                    DisplayName = "Wyświetl wartość sprzedaży dla każdego pracownika",
                    Action = () => 
                    {
                        Methods.DisplayEmployeesSummary(esvm);
                        Console.Write("Cofnij.... ");
                        Program.Cofnij();
                    }
                },



                 new MenuItem
                {
                    Order = 2,
                    DisplayName = "Wyświetl liste produktów z największym rabatem",
                    Action = () => 
                        {
                        Methods.DisplayProductsTopDiscounts(ptdvm);
                        Console.Write("Cofnij...");
                        Program.Cofnij();
                        }
                },




                 new MenuItem
                {
                    Order = 3,
                    DisplayName = "Wyświetl liste sprzedanych produktów",
                    Action = () => 
                        {
                        Methods.DisplayTotalProductsSold(tpsvm);
                        Console.Write("Cofnij...");
                        Program.Cofnij();
                        }
                },




                 new MenuItem
                {
                    Order = 4,
                    DisplayName = "Wyświetl stan magazynu",
                    Action = () => 
                        
                    {
                            Methods.DisplayUnitsInStock(uisvm);
                            Console.Write("Cofnij...");
                            Program.Cofnij();

                    }
                },






                  new MenuItem
                {
                    Order = 5,
                    DisplayName = "Wyświetl klientów",
                    Action = () => 
                        {
                        Methods.DisplayCustomersSummary(csvm);
                        Console.Write("Cofnij...");
                        Program.Cofnij();
                        }
                                          


                },





                 new MenuItem
                {
                    Order = 6,
                    DisplayName = "CRUD",
                    Action = () => 

                        {
                        MenuManager menu3 = new MenuManager(new MenuItem[]
                        {
                            new MenuItem
                            {
                                Order = 1,
                                DisplayName = "Create",
                                Action = () => 
                                    {
                                        crud.NewData();
                                        Console.Write("Cofnij...");
                                        Program.Cofnij();
                                    }
                            },

                            new MenuItem
                            {
                                Order = 2,
                                DisplayName = "Read",
                                Action = () => 
                                    {
                                        crud.ReadCustomers();
                                        Console.Write("Cofnij...");
                                        Program.Cofnij();
                                    }
                            },

                            new MenuItem
                            {
                                Order = 3,
                                DisplayName = "Update",
                                Action = () => 
                                    {
                                        crud.UpdateCustomer();
                                        Console.Write("Cofnij...");
                                        Program.Cofnij();
                                    }
                            },

                             new MenuItem
                            {
                                Order = 4,
                                DisplayName = "Delete",
                                Action = () => 
                                    {
                                        crud.DeleteCustomer();
                                        Console.Write("Cofnij...");
                                        Program.Cofnij();
                                    }
                            },
                        });
                        menu3.Run();

                    }
                },

               




            });

           menu.Run();
        }

        public static void Cofnij()
        {
            Console.ReadLine();
            Console.Clear();
            menu.Run();
        }
    }
}
